<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/1
 * Time: 下午6:55
 */
namespace App\Busi\Modules\Auth;
use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\CaptchaTrait;
use App\Busi\Traits\UserTrait;
use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Http\Requests\AuthRegRo;
use App\Models\User;
use App\Util\StringUtil;

class RegisterBusi extends BaseBusi
{
    use UserTrait,CaptchaTrait;
    public function handle(AuthRegRo $ro){
        $phone = $ro->phone;
        $password = $ro->password;
        $this->checkCaptcha('captcha:register:'.$ro->phone, $ro->captcha);
        $this->existsPhone($phone);
        if ($this->getUserByPhone($phone)) {
            UserException::touch(UserError::PHONE_EXIST);
        }
        $nickname = '奇快企服用户'.substr($phone,7);
        $model = new User();

        $uid = StringUtil::generateUid();
        $model->uid = $uid;
        $model->name = $nickname;
        $model->phone = $phone;
        $model->phone = $phone;
        $model->password = md5($password);
        $model->avatar = "http://poster.vrpanduola.com/logo.png";
        $model->company_id = 0;
        $model->show_order = 0;
        $rs = $model->saveOrFail();
        $data = array(
            'uid' =>$uid,
            'username' => $nickname,

            'phone'=> $phone

        );

        return $data;

    }
}